﻿namespace Ruirun.Mes
{
    /// <summary>
    /// 程序启动类
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 配置服务，此方法由运行时调用。使用此方法可将服务添加到容器中。
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services">指定服务描述符集合的协定</param>
        public void ConfigureServices(IServiceCollection services)
        {
            //添加数据库开发人员页面异常筛选器
            services.AddDatabaseDeveloperPageExceptionFilter();

            //自动加载所有模块(可排除指定类型的模块)
            //services.BuildeMicroCloud().AddPacks(typeof(HangfirePack), typeof(RedisPack));
            //手动加载模块
            services.BuildeMicroCloud()
                // 必要加载模块
                .AddPack<Log4NetPack>()
                .AddPack<AutoMapperPack>()
                .AddPack<SignalRPack>()
                .AddPack<EndpointsPack>() //已设置为自动启动模块
                .AddPack<MiniProfilerPack>()
                .AddPack<SwaggerPack>()
                .AddPack<DefaultDbContextMigrationPack>()
                .AddPack<ProjectDbContextMigrationPack>()
                .AddPack<HangfirePack>()
                .AddPack<AuthenticationPack>()
                .AddPack<FunctionAuthorizationPack>()
                .AddPack<DataAuthorizationPack>()
                .AddPack<AuditPack>()
                .AddPack<MicroCloudMessagePack>()   //注：基于SignalR模块的消息模块。
                .AddPack<SmsPack>()
                .AddPack<SystemsPack>()
                .AddPack<MesPack>();
        }

        /// <summary>
        /// 配置，此方法由运行时调用。使用此方法配置HTTP请求管道。
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app">用于配置HTTP管道和路由的web应用程序</param>
        public void Configure(WebApplication app)
        {
            // 有关运行应用程序的web宿主环境的信息
            IWebHostEnvironment env = app.Environment;

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
                //app.UseHttpsRedirection(); // 启用HTTPS
            }

            //app.UseMiddleware<HostHttpCryptoMiddleware>();
            //app.UseMiddleware<JsonNoFoundHandlerMiddleware>();
            app.UseMiddleware<JsonExceptionHandlerMiddleware>() // 移除异常中间件JsonExceptionHandlerMiddleware，有异常直接返回给前端，由前端捕捉
                .UseDefaultFiles()
                .UseStaticFiles();
            app.UseMicroCloud();

        }

    }
}